package com.tiancheng.trade.authserver.controller;

import cn.dev33.satoken.context.SaHolder;
import com.tiancheng.trade.commom.web.configuration.PlatformRequestProperties;
import com.tiancheng.trade.commom.core.data.Result;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.utils.URIBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Enumeration;

/**
 * @ClassName LoginController
 * @Description 登录
 * @Author kellen
 * @Date 2024/10/20 23:05
 */
@Slf4j
@Controller
public class LoginController {

    @Resource
    private PlatformRequestProperties platformRequestProperties;

    // 构建登录页面链接
    @RequestMapping("/login")
    @ResponseBody
    public Result<String> login(HttpServletRequest request) throws URISyntaxException {
        log.info("------- 去登录:{} ", SaHolder.getRequest().getUrl());
        URIBuilder builder = new URIBuilder(platformRequestProperties.getLoginUrl());
        //获取所有的请求参数
        Enumeration<String> paraNames = request.getParameterNames();
        for (Enumeration<String> e = paraNames; e.hasMoreElements(); ) {
            String thisName = e.nextElement().toString();
            String thisValue = request.getParameter(thisName);
            builder.addParameter(thisName, thisValue);
        }
        return Result.success(setAuthParams(builder.toString()));
    }

    private String setAuthParams(String url) {
        url += ("&client_id=" + platformRequestProperties.getClientId());
        url += ("&state=tiancheng_oauth2");
        url += ("&scope=userBase");
        url += ("&response_type=code");
        return url;
    }

    // OAuth2-Server 端：处理所有 OAuth2 相关请求
    @RequestMapping("/login/ui")
    public Object loginPage(HttpServletRequest request) {
        log.info("------- 去登录:{} ", SaHolder.getRequest().getUrl());
        return "login";
    }


    // OAuth2-Server 端：处理所有 OAuth2 相关请求
    @RequestMapping("/login/callback")
    @ResponseBody
    public Result<String> callback(HttpServletRequest request) throws URISyntaxException {
        log.info("------- 去登录:{} ", SaHolder.getRequest().getUrl());
        URIBuilder builder = new URIBuilder(platformRequestProperties.getLoginUrl());
        //获取所有的请求参数
        Enumeration<String> paraNames = request.getParameterNames();
        for (Enumeration<String> e = paraNames; e.hasMoreElements(); ) {
            String thisName = e.nextElement().toString();
            String thisValue = request.getParameter(thisName);
            builder.addParameter(thisName, thisValue);
        }
        return Result.success(builder.toString());
    }


    /**
     * oauth2登陆回调
     */
    @GetMapping("/oauth2Callback")
    public Result<String> oauth2Callback(@RequestParam String state) throws IOException {
        return Result.success("");
//        String callbackUrl = this.service.getOauth2SignInCallbackUrl(state);
//        if (callbackUrl == null) {
//            throw new ErrorCodeException(ErrorCodeConstant.paramCheckErrorCode, "登陆请求已过期");
//        }
//        String query = this.request.getQueryString();
//        if (StringUtils.isNotEmpty(query)) {
//            if (callbackUrl.contains("?")) {
//                callbackUrl += "&" + query;
//            } else {
//                callbackUrl += "?" + query;
//            }
//        }
//        if (HttpServletUtils.isAjax(this.request)) {
//            return Result.success(callbackUrl);
//        } else {
//            this.response.sendRedirect(callbackUrl);
//            return null;
//        }
    }
}
